package com.zrrd.mq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author WanJl
 * @version 1.0
 * @title BandingUtil
 * @description
 * @create 2024/7/20
 */
//@Configuration
public class ErrorMessageConfig {
    /**
     * 创建一个专门负责失败消息的交换机
     * @return
     */
    @Bean
    public DirectExchange errorMessageExchange(){
        return new DirectExchange("error.direct");
    }

    /**
     * 创建一个专门负责失败消息的队列
     * @return
     */
    @Bean
    public Queue errorQueue(){
        Queue queue = new Queue("error.queue", true);
        return queue;
    }

    /**
     * 将交换机和队列进行绑定，并指定路由键为 error
     * @param errorQueue
     * @param errorMessageExchange
     * @return
     */
    @Bean
    public Binding errorBinding(Queue errorQueue, DirectExchange errorMessageExchange){
        return BindingBuilder.bind(errorQueue).to(errorMessageExchange).with("err6666or");
    }

    /**
     * 设置当其他的队列里的消息递交到消费者后，消费者消费者消息失败重试次数达到限制后。将消息发送给指定交换机
     * @param rabbitTemplate
     * @return
     */
    @Bean
    public MessageRecoverer republishMessageRecoverer(RabbitTemplate rabbitTemplate){
        return new RepublishMessageRecoverer(rabbitTemplate, "error.direct",
                "err6666or");
    }
}
